//
//  HomeTableTableView.m
//  OCMicroBlog
//
//  Created by 002 on 15/11/4.
//  Copyright © 2015年 002. All rights reserved.
//

#import "HomeTableTableView.h"
#import "NetworkTools.h"
#import "SVProgressHUD.h"
#import "StatusListViewModel.h"
#import "Status.h"
#import "StatusCell.h"
#import "StatusRetweetedCell.h"
#import "StatusNormalCell.h"
#import "WBRefreshControl.h"
#import "UILabel+Extention.h"
#import "UIView+Extention.h"

@interface HomeTableTableView ()

@property(nonatomic,strong) StatusListViewModel *listViewModel;

// 上拉刷新视图
@property(nonatomic,strong) UIActivityIndicatorView *pullupView;

@end

@implementation HomeTableTableView

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.visitorLoginView setupInfoWithImageName:nil message:@"关注一些人，回这里看看有什么惊喜"];
    
    // 准备表格
    [self prepareTableView];
    
    // 加载数据
    [self loadData];
    
}

// 加载数据
- (void)loadData {
    
    // 开始刷新
    [self.refreshControl beginRefreshing];
    
    [self.listViewModel loadStatus:self.pullupView.isAnimating finished:^(BOOL isSuccessed) {
        
        // 关闭刷新控件
        [self.refreshControl endRefreshing];
        
        [self.pullupView stopAnimating];
        
        NSLog(@"refreshControl:%@",self.refreshControl);
        
        if (!isSuccessed) {
            
            [SVProgressHUD showErrorWithStatus:@"您的网络不给力"];
            return ;
        }
        // 成功则刷新数据
//        NSLog(@"result:%@",self.listViewModel.statusList);
        [self.tableView reloadData];
    }];

}

// 返回行高 - 根据微博内容动态确定
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return self.listViewModel.statusList[indexPath.row].rowHeight;
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    NSLog(@"%zd",self.listViewModel.statusList.count);
    return self.listViewModel.statusList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 1. 取模型数据
    StatusViewModel *vm = [self listViewModel].statusList[indexPath.row];
    
    // 2. 从缓存池子取，没有自动创建cell
    StatusCell *cell = [tableView dequeueReusableCellWithIdentifier:vm.cellID forIndexPath:indexPath];
    
    // 3. 给cell赋值
    cell.viewModel = vm;
    
    // 判断是否需要上拉刷新
    if (indexPath.row == self.listViewModel.statusList.count - 1 && ![self.pullupView isAnimating]) {
        NSLog(@"应该上拉刷新");
        [self.pullupView startAnimating];
        [self loadData];
    }
    
    // 4. 返回cell
    return cell;
}

#pragma mark -

// 显示下拉刷新提示
- (void)showPulldownTips {

    NSInteger count = self.listViewModel.pulldownCount;
    if (!(count > 0)) {
        NSLog(@"刷新到了%zd条数据",count);
    } else {
        NSLog(@"没有刷新到数据");
    }
    
}

- (StatusListViewModel *)listViewModel {
    if (!_listViewModel) {
        _listViewModel = [[StatusListViewModel alloc] init];
    }
    return _listViewModel;
}

#pragma mark - 准备表格
- (void)prepareTableView {
    
    [self.tableView registerClass:[StatusRetweetedCell class] forCellReuseIdentifier:StatusRetweetedCellId];
    [self.tableView registerClass:[StatusNormalCell class] forCellReuseIdentifier:StatusCellNormalId];
    
    // 取消分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // 设置预估行高
    self.tableView.estimatedRowHeight = 400;
    
    // 创建 |下拉| 刷新视图
    self.refreshControl = [[WBRefreshControl alloc] init];
    
    // |上拉| 刷新控件
    self.tableView.tableFooterView = self.pullupView;
    
    // 添加监听方法
    [self.refreshControl addTarget:self action:@selector(loadData) forControlEvents:UIControlEventValueChanged];
    
    // 设置 tintColor
    self.refreshControl.tintColor = [UIColor clearColor];
}

#pragma mark - 加载刷新数据
- (UIActivityIndicatorView *)pullupView {
    if (!_pullupView) {
        _pullupView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        _pullupView.color = [UIColor lightGrayColor];
    }
    return _pullupView;
}

@end
